package com.example.jiaju.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.room.Room;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.jiaju.R;
import com.example.jiaju.adapter.ARModelAdapter;
import com.example.jiaju.dao.ConsultDao;
import com.example.jiaju.database.ConsultDaoDatabase;
import com.example.jiaju.databinding.ActivityConcreteHouseBinding;
import com.example.jiaju.entity.Consult;
import com.example.jiaju.entity.House;
import com.example.jiaju.ui.msg.ChatActivity;
import com.example.jiaju.utils.MyItemDecoration;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ConcreteHouseActivity extends AppCompatActivity {


    private String TAG="ConcreteHouseActivity";
    private House.PageDTO.ListDTO houseListDTO;
    private List<String> arModelList=new ArrayList<>();
    private ActivityConcreteHouseBinding binding;
    private ARModelAdapter adapter;
    private RecyclerView recyclerView;
    private ConsultDao dao;
    private ConsultDaoDatabase database;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //dataBinding
        binding= DataBindingUtil.setContentView(this,R.layout.activity_concrete_house);
        //沉浸式状态栏
        ImmersionBar.with(this).transparentBar().init();

        //获取上个页面的数据
        houseListDTO= (House.PageDTO.ListDTO) getIntent().getSerializableExtra("houseListDTO");
        Log.d(TAG, "onCreate: "+houseListDTO);
        //填充界面
        binding.houseAddress.setText(houseListDTO.houseAdd);
        Glide.with(this).load(houseListDTO.houseImg).into(binding.houseBgImg);
        binding.houseName.setText(houseListDTO.houseName);
        binding.housePrice.setText(houseListDTO.housePrice.toString()+"元/平");
        binding.housePhoneText.setText(houseListDTO.housePhone);

        //获取ar户型的图片链接集合
        String tempString=houseListDTO.houseModelImg;
        String[] strings=tempString.split(",");
        for(int i=0;i<strings.length;++i){
            arModelList.add(strings[i]);
        }
        //填充AR户型
        recyclerView=binding.arModelRecyclerview;
        recyclerView.setVisibility(View.GONE);
        adapter=new ARModelAdapter(arModelList,ConcreteHouseActivity.this);
        recyclerView.setAdapter(adapter);
        recyclerView.setVisibility(View.VISIBLE);
        recyclerView.addItemDecoration(new MyItemDecoration(8));
        recyclerView.setLayoutManager(new LinearLayoutManager(ConcreteHouseActivity.this,
                LinearLayoutManager.HORIZONTAL,false));

        //回到HouseActivity页面
        binding.returnBtn.setOnClickListener(v -> finish());
        //跳转到chatActivity
        binding.chatBtn.setOnClickListener(v -> {
            Intent intent=new Intent(ConcreteHouseActivity.this, ChatActivity.class);
            intent.putExtra("consult",new Consult("", houseListDTO.houseName, ""));
            //1.和数据库建立连接
            database= Room.databaseBuilder(ConcreteHouseActivity.this, ConsultDaoDatabase.class,"consult_database")
                    .allowMainThreadQueries()//允许在主线程调用
                    .build();
            //2.返回的不是一个接口对象，返回的是一个接口的实现类  是运行期间动态生成的
            dao=database.getConsultDao();
            //3.操作数据库
            Consult consult=new Consult(houseListDTO.houseImg,houseListDTO.houseName+"经理","您好，请问有什么能为你服务的？");
            dao.insertConsult(consult);
            startActivity(intent);
        });
        //拨打电话
        binding.callBtn.setOnClickListener(v -> XXPermissions.with(ConcreteHouseActivity.this).
            permission(Permission.CALL_PHONE).
            request((permissions, all) -> {
                Toast.makeText(ConcreteHouseActivity.this,"拨号权限授予成功",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:17377821609"));
                startActivity(intent);
            }));
    }
}